package com.ewiqi.springsecuritydemo.contoller;

import com.ewiqi.springsecuritydemo.model.Permission;
import com.ewiqi.springsecuritydemo.model.Role;
import com.ewiqi.springsecuritydemo.model.User;
import com.ewiqi.springsecuritydemo.service.SecurityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author fengyu.zhang
 * @date 2021/1/19 1:44 下午
 */
@RestController
@RequestMapping("/security")
//@Secured("admin")
public class SecurityController {
    @Resource
    SecurityService securityService;

    @PostMapping("/permission")
    public String createPermission(@RequestBody Permission permission) {
        securityService.createPermission(permission);
        return "success";
    }

    @GetMapping("/permission/{id}")
    public Permission loadPermission(@PathVariable int id) {
        return securityService.loadPermission(id);
    }

    @GetMapping("/permission")
    public List<Permission> loadPermission() {
        return securityService.loadPermission();
    }

    @DeleteMapping("/permission/{id}")
    public String deletePermission(@PathVariable int id) {
        securityService.deletePermission(id);
        return "success";
    }

    @PutMapping("/permission")
    public String updatePermission(@RequestBody Permission permission) {
        securityService.updatePermission(permission);
        return "success";
    }

    @PostMapping("/role")
    public String createRole(@RequestBody Role role) {
        securityService.createRole(role);
        return "success";
    }

    @PutMapping("/role")
    public String updateRole(@RequestBody Role role) {
        securityService.updateRole(role);
        return "success";
    }

    @DeleteMapping("/role/{id}")
    public String deleteRole(@PathVariable int id) {
        securityService.deleteRole(id);
        return "success";
    }

    @GetMapping("/role/{id}")
    public Role loadRole(@PathVariable int id) {
        return securityService.loadRole(id);
    }

    @GetMapping("/role")
    public List<Role> loadRole() {
        return securityService.loadRole();
    }

    @PostMapping("/user")
    public String createUser(@RequestBody User user) {
        securityService.createUser(user);
        return "success";
    }

    @PutMapping("/user")
    public String updateUser(@RequestBody User user) {
        securityService.updateUser(user);
        return "success";
    }

    @DeleteMapping("/user/{id}")
    public String deleteUser(@PathVariable int id) {
        securityService.deleteUser(id);
        return "success";
    }

    @GetMapping("/user/{id}")
    public User loadUser(@PathVariable int id) {
        return securityService.loadUser(id);
    }

    @GetMapping("/user")
    public List<User> loadUser() {
        return securityService.loadUser();
    }
}
